


_cart ={

    getCart_item:function (pageSize,pageNum) {

        $.ajax({
            type:'GET',
            url: _common_util.getServerURL('cart/cart_item'),
            dataType:'json',
            data:{
              'pageSize':pageSize,
              'pageNum' : pageNum
            },
            success:function (res) {
                console.log(res);
                if (res.status === 3 ){
                    window.wxc.xcConfirm('尚未登录，请登录','info');
                    window.location.href = 'my-account.html';
                }else if (res.status === 0){
                    console.log('购物车'+res);
                    $('#cart-item-content').html('');


                    //遍历list添加商品
                    $.each(res.data.cartCommodityVOList,function (i,item) {


                        let imageSrc = _common_util.getImageUrl(item.image);
                        let totalprice = item.price * item.quantity;
                        let str ='                                             <tr class="cart_item" id="'+item.itemId+'">\n' +
                            '\n' +
                            '\n' +
                            '                                                <td>\n' +
                            '                                                    <div class="checkbox">\n' +
                            '                                                        <input type="checkbox" id="'+item.id+'checkbox"  itemId="'+item.id+'" onclick="changeChecked(this)">\n' +
                            '                                                    </div>\n' +
                            '                                                </td>\n' +
                            '\n' +
                            '\n' +
                            '                                                <td class="product-thumbnail">\n' +
                            '                                                    <a href="single-product.html?itemId='+item.itemId+'"><img width="180" height="180" src="'+imageSrc+'" alt=""></a>\n' +
                            '                                                </td>\n' +
                            '\n' +
                            '                                                <td data-title="Product" class="product-name">\n' +
                            '                                                    <a href="single-product.html?itemId='+item.itemId+'">'+item.itemName+'</a>\n' +
                            '                                                </td>\n' +
                            '\n' +
                            '                                                <td data-title="Price" class="product-price">\n' +
                            '                                                    <span class="amount" id="'+item.id+'price">'+item.price+'</span>\n' +
                            '                                                </td>\n' +
                            '\n' +
                            '                                                <td data-title="Quantity" class="product-quantity">\n' +
                            '                                                    <div class="quantity buttons_added">' +
                            '                                                        <input type="button" class="minus" onclick="minus(this)" itemId="'+item.id+'" value="-">\n' +
                            '                                                        <label>Quantity:</label>\n' +
                            '                                                        <input type="number" size="4" onchange="check('+item.id+')" class="input-text qty text" title="Qty" id="'+item.id+'quantity" value="'+item.quantity+'"  max="29" min="0" step="1">\n' +
                            '                                                        <input type="button" class="plus" onclick="add(this)" itemId="'+item.id+'"  value="+">\n' +
                            '                                                    </div>\n' +
                            '                                                </td>\n' +
                            '\n' +
                            '                                                <td data-title="Total" class="product-subtotal">\n' +
                            '                                                    <span class="amount" id="'+item.id+'totalprice">'+totalprice+'</span>\n' +
                            '                                                </td>\n' +
                            '\n' +
                            '                                                <td class="product-remove">\n' +
                            '                                                   <a class="remove" style="font-size: 30px;" onclick="deleteCartItem(this)" itemId="'+item.id+'">×</a>\n' +
                            '                                                </td>\n' +
                            '                                            </tr>\n ';

                        $('#cart-item-content').append(str);


                        if (item.checked === 0){
                            $('#'+item.id+'checkbox').prop("checked",true);
                        }else {
                            $('#'+item.id+'checkbox').prop("checked",false);
                        }
                    })

                    $('#allprice').html(res.data.allPrice);
                    $('#confirmed-allprice').html(res.data.allPrice);

                    //分页
                    totalpage = res.data.endRow;
                    currentpage = res.data.pageNum;
                    prepage = res.data.prePage;
                    nextpage = res.data.nextPage;
                    restpage = totalpage - currentpage;
                    console.log("剩余的页数："+restpage);
                    let pagestr='';

                    if (currentpage>3){
                        //1和省略号
                        pagestr = pagestr+'               <li><a onclick="toPage(this)" page="1" class="page-numbers">1</a></li> \n' +
                            '                              <li class="apostrophe">...</li>' +
                            '                             <li><a onclick="toPage(this)" page="'+prepage+'" class="page-numbers">'+prepage+'</a></li>';
                    }else {
                        for (var i = 1; i<currentpage;i++) {
                            pagestr = pagestr + '                <li><a onclick="toPage(this)" page="' + i + '" class="page-numbers">' + i + '</a></li>';
                        }
                    }
                    pagestr = pagestr + '                   <li><a onclick="toPage(this)" page="'+currentpage+'" class="page-numbers current">'+currentpage+'</a></li>\n';
                    if (restpage>2){
                        pagestr = pagestr + '               <li><a onclick="toPage(this)" page="'+nextpage+'" class="page-numbers">'+nextpage+'</a></li>       ' +
                            '               <li class="apostrophe">...</li>\n' +
                            '               <li><a onclick="toPage(this)" page="'+totalpage+'" class="page-numbers">'+totalpage+'</a></li>';
                    }else if (nextpage != totalpage && res.data.hasNextPage) {

                        pagestr = pagestr + '               <li><a onclick="toPage(this)" page="'+nextpage+'" class="page-numbers">'+nextpage+'</a></li> ' +
                            '               <li><a onclick="toPage(this)" page="'+totalpage+'" class="page-numbers">'+totalpage+'</a></li> ';
                    }else if (nextpage === totalpage){
                        pagestr = pagestr + ' <li><a onclick="toPage(this)" page="'+totalpage+'" class="page-numbers">'+totalpage+'</a></li>';
                    }
                    $('#page-numbers').html(pagestr);
                }
            }
        })
    }


}


function changeChecked(item) {
    let id = $(item).attr('itemId');
    if ($(item).is(":checked")== true) {
        console.log('checked');
        $.ajax({
            type:'POST',
            url: _common_util.getServerURL('cart/update_checked'),
            dataType:'json',
            data:{
                'id' : id,
                'checked' : 0
            },
            success:function (res) {
                if (res.status === 0){
                    console.log('成功变为选中状态')
                    let producttotalprice =  $('#'+id+'totalprice').html();
                    let allprice = $('#allprice').html();
                    newAllprice = allprice*1 + producttotalprice*1 ;
                    $('#allprice').html(newAllprice);

                    $('#'+id+'checkbox').prop("checked",true);
                }

            }
        })
    } else {
        //取消选中触发事件
        console.log('nonecheck');
        $.ajax({
            type:'POST',
            url: _common_util.getServerURL('cart/update_checked'),
            dataType:'json',
            data:{
                'id' : id,
                'checked' : 1
            },
            success:function (res) {
                if (res.status === 0){
                    console.log('非选中状态')
                    let producttotalprice =  $('#'+id+'totalprice').html();
                    let allprice = $('#allprice').html();
                    newAllprice = allprice - producttotalprice
                    $('#allprice').html(newAllprice);

                    $('#'+item.id+'checkbox').prop("checked",false);
                }

            }
        })
    }
}


function toPage(item){
    let page =$(item).attr('page');
    let keyword =$('#input-search').val();
    console.log('cccccpage:'+page);
    _cart.getCart_item('',page);
}


function minus (item){
    let itemId = $(item).attr('itemId');
    console.log("minus"+itemId);

    let quantity = $('#'+itemId+'quantity').val();
    let pre = quantity*1 - 1 ;
    console.log("pre"+pre);
    let price = $('#'+itemId+'price').html();
    let totalprice = pre * price;
    let beforeallprice =$('#allprice').html();
    let allprice =  beforeallprice*1 - price*1 ;

    if (pre<=0){
        console.log('不能小于0');
    }else {
        //判断是否合格
        $.ajax({
            type: 'POST',
            url: _common_util.getServerURL('cart/add_quantity'),
            dataType:'json',
            data:{
                'id' : itemId,
                'quantity': pre
            },
            success:function (res) {
                console.log(res);
                if (res.status === 0){
                    $('#'+itemId+'quantity').val(pre);
                    $('#'+itemId+'totalprice').html(totalprice);
                    $('#allprice').html(allprice);
                }
            }
        })
    }





}

function add (item){
    let itemId = $(item).attr('itemId');
    console.log("add"+itemId);

    let quantity = $('#'+itemId+'quantity').val();
    let pre = quantity*1 + 1 ;
    let price = $('#'+itemId+'price').html();
    let totalprice = pre * price
    let beforeallprice =$('#allprice').html();
    let allprice =  beforeallprice*1 + price*1 ;
    console.log("pre"+pre);
    //判断是否合格
    $.ajax({
        type: 'POST',
        url: _common_util.getServerURL('cart/add_quantity'),
        dataType:'json',
        data:{
            'id' : itemId,
            'quantity': pre
        },
        success:function (res) {
            console.log(res);
            if (res.status === 0){
                $('#'+itemId+'quantity').val(pre);
                $('#'+itemId+'totalprice').html(totalprice);
                $('#allprice').html(allprice);
            }
        }
    })

}

function check(itemId){
    let quantity = $('#'+itemId+'quantity').val();
    console.log('check')
    $.ajax({
        type: 'POST',
        url: _common_util.getServerURL('cart/add_quantity'),
        dataType:'json',
        data:{
            'id' : itemId,
            'quantity': quantity
        },
        success:function (res) {
            console.log(res);
            if (res.status === 0){
                console.log('商品数量修改成功');
            }else if (res.status === 3){
                window.wxc.xcConfirm('尚未登录','info');
            }else if (res.status === 2){
                 console.log('修改失败');
                $.ajax({
                    type : 'GET',
                    url  : _common_util.getServerURL('commodity/item_detail')+'?itemId='+itemId ,
                    success : function(res){
                        if (res.status === 0 ){
                            let str = '最大库存为：'+res.data.stock+'已将商品数量修改';
                            window.wxc.xcConfirm( str,'info');
                            $('#'+itemId+'quantity').val(res.data.stock);
                        }
                    }

                })


            }
        }
    })
}


function deleteCartItem(item){
    let itemId = $(item).attr('itemId');
    $.ajax({
        type:'GET',
        url: _common_util.getServerURL('cart/remove_item?id=')+itemId,

        success:function (res) {
            console.log(res);
            if (res.status === 0 ){
                console.log('成功删除');
                window.wxc.xcConfirm('移除成功','info')
                $('#'+itemId).css('pointer-events','none');
                _cart.getCart_item('','');

            }else {
                console.log('删除失败');
            }
        }
    })
}



function updateCart(){
    _cart.getCart_item();

}


$(function () {
    _cart.getCart_item();

    $('#submit-order').on('click',function () {

        $.ajax({
            type:'POST',
            url : _common_util.getServerURL('cart/purchase_checked'),
            success:function (res) {
                console.log(res)
                if (res.status === 0) {
                    window.wxc.xcConfirm('生成订单，即将跳转至订单页面', 'info', {
                        onOk: function () {
                            window.location.href = 'my-order.html';
                        }
                    })
                }
            }
        })
    })
    
})



// let str ='                                             <tr class="cart_item">\n' +
//     '\n' +
//     '\n' +
//     '                                                <td>\n' +
//     '                                                    <div class="checkbox">\n' +
//     '                                                        <input type="checkbox"  value="'+item.itemId+'">\n' +
//     '                                                    </div>\n' +
//     '                                                </td>\n' +
//     '\n' +
//     '\n' +
//     '                                                <td class="product-thumbnail">\n' +
//     '                                                    <a href="single-product.html?itemId='+item.itemId+'"><img width="180" height="180" src="assets/images/products/2.jpg" alt=""></a>\n' +
//     '                                                </td>\n' +
//     '\n' +
//     '                                                <td data-title="Product" class="product-name">\n' +
//     '                                                    <a href="single-product.html?itemId='+item.itemId+'">'+item.itemName+'</a>\n' +
//     '                                                </td>\n' +
//     '\n' +
//     '                                                <td data-title="Price" class="product-price">\n' +
//     '                                                    <span class="amount">'+item.price+'</span>\n' +
//     '                                                </td>\n' +
//     '\n' +
//     '                                                <td data-title="Quantity" class="product-quantity">\n' +
//     '                                                    <div class="quantity buttons_added">' +
//     '                                                        <input type="button" class="minus" onclick="minus('+item.itemId+')" value="-">\n' +
//     '                                                        <label>Quantity:</label>\n' +
//     '                                                        <input type="number" size="4" class="input-text qty text" title="Qty" id="'+item.itemId+'quantity" value="'+item.quantity+'" name="cart[92f54963fc39a9d87c2253186808ea61][qty]" max="29" min="0" step="1">\n' +
//     '                                                        <input type="button" class="plus" onclick="add('+item.itemId+')" value="+">\n' +
//     '                                                    </div>\n' +
//     '                                                </td>\n' +
//     '\n' +
//     '                                                <td data-title="Total" class="product-subtotal">\n' +
//     '                                                    <span class="amount" id="'+item.itemId+'totalprice">'+item.price+'</span>\n' +
//     '                                                </td>\n' +
//     '\n' +
//     '                                                <td class="product-remove">\n' +
//     '                                                    <a class="remove" href="#">×</a>\n' +
//     '                                                </td>\n' +
//     '                                            </tr>\n ';